<?php
/**
 * Created by PhpStorm.
 * User: zhenghao
 * Date: 2015/6/17
 * Time: 13:13
 */

require_once __DIR__."/adodb5/adodb.inc.php";

class MyAdodb {

    private $conn;

    public function __construct(){

        global $conf;

        try{

            //设置数据库类型
            $this->conn=ADONewConnection($conf["DB_TYPE"]);

            //打开连接
            $this->conn->Connect($conf["DB_connect_url"],$conf["DB_connect_root"],$conf["DB_connect_password"],$conf["DB_connect_db_name"]);

            //设置编码格式
            $this->conn->Execute("set names ".$conf['DB_CHAR_SET']);
            $this->conn->Execute("set character_set_client=".$conf['DB_CHAR_SET']);
            $this->conn->Execute("set character_set_results=".$conf['DB_CHAR_SET']);

            //设置查询结果集读取方式
            $this->conn->SetFetchMode(ADODB_FETCH_ASSOC);

        }catch (Exception $e){

            print_sql_log("ERROR",$e->getMessage());
        }

    }


    //切换数据库连接
    public function changeDb($url,$root,$password,$db_name,$char_set="utf8"){

        //先关闭连接
        $this->close();

        //打开新连接
        $this->conn->Connect($url,$root,$password,$db_name);

        //设置编码格式
        $this->conn->Execute("set names ".$char_set);
        $this->conn->Execute("set character_set_client=".$char_set);
        $this->conn->Execute("set character_set_results=".$char_set);

    }


    //插入inset into
    public function insert($table,$str){
        //$conn->debug=1;
        if (empty($table) || empty($str)) return 0;
        $str = array_diff($str, array(null));	//去除数组中空值

        print_sql_log("Insert table:".$table);
        print_sql_log($str);

        $rs = $this->conn->AutoExecute($table,$str,'INSERT');
        //$insertSQL = $conn->GetInsertSQL($rs, $str,true);
        //echo $insertSQL;
        if ($rs===false) return 0;//{print $conn->ErrorMsg();}
        else return $this->conn->Insert_ID();
    }

    public function insertnoreturn($table,$str){//插入inset into
        //$conn->debug=1;
        if (empty($table) || empty($str)) return 0;
        $str = array_diff($str, array(null));	//去除数组中空值
        $rs = $this->conn->AutoExecute($table,$str,'INSERT');
        //$insertSQL = $conn->GetInsertSQL($rs, $str,true);
        //echo $insertSQL;
    }

    public function update($table,$str,$where) {//update数据更新
        if (empty($table) || empty($str)) return 0;

        print_sql_log("update table:$table");
        print_sql_log($str);
        print_sql_log($where);

        //	$str = array_diff($str, array(null));	//去除数组中空值
        $rs = $this->conn->AutoExecute($table,$str,'UPDATE',$where);

        if ($rs===false){print $this->$conn->ErrorMsg();}	//报错信息
        else return $rs;
    }


    //取得记录总数
    public function get_count($table,$str){

        if (empty($table) || empty($str)) return 0;
        if (!isset($str['where'])) {
            $where = '';
        }else {
            $where = 'where ' . $str['where'];
        }
        if (!isset($str['group'])) {
            $group = '';
        }else {
            $group = 'group by ' . $str['group'];
        }

        //$sql="SELECT * FROM $table $where $group";
        $sql="SELECT count(*) as n FROM $table $where";

        $rs = $this->conn->CacheExecute($sql);

        if ($rs->fields['n'] > 0) return $rs->fields['n'];
        else return 0;

    }

    //取得单笔数据的信息	//2011-03-14
    public function get_Id($table,$where,$fields){
        if (empty($table) || empty($where)) return 0;
        if (empty($fields)) {
            $fields = '*';
        }

        $sql="SELECT $fields FROM $table where $where";

        $rs = $this->conn->CacheExecute($sql);

        if (!$rs->EOF){
            $data = $rs->FetchRow();

            return $data;

        } else {
            return false;
        }

    }

    //取的所有数据集合
    public function select($table,$str){
        if (empty($table) || empty($str)) return 0;
        if (!isset($str['fields'])) {
            $fields = '*';
        }else {
            $fields = $str['fields'];
        }
        if (!isset($str['where'])) {
            $where = '';
        }else {
            $where = 'where ' . $str['where'];
        }
        if (!isset($str['order'])) {
            $order = '';
        }else {
            $order = 'order by ' . $str['order'];
        }
        if (!isset($str['group'])) {
            $group = '';
        }else {
            $group = 'group by ' . $str['group'];
        }
        $sql = "SELECT $fields FROM $table $where $group $order";
        //echo $sql;
        //modified by JEFF 2011-8-15,add filters sort direction and record count
        if(isset($str['sort']))
        {
            $sort=$str['sort'];
            $sql.=" $sort";
        }
        if(isset($str['count']))
        {
            $count=$str['count'];
            $sql.=" limit 0,$count";
        }
        //echo $sql;

        //字符串格式转换
        print_sql_log($sql);

        $data = $this->conn->CacheExecute($sql)->GetAll();

        $this->close();

        return $data;
    }

    //limit数据集合
    public function get_limit($table,$str,$num,$start) {
        if (empty($table) || empty($str)) return 0;
        if (!isset($str['fields'])) {
            $fields = '*';
        }else {
            $fields = $str['fields'];
        }
        if (!isset($str['where'])) {
            $where = '';
        }else {
            $where = 'where ' . $str['where'];
        }
        if (!isset($str['order'])) {
            $order = '';
        }else {
            $order = 'order by ' . $str['order'];
        }
        if (!isset($str['group'])) {
            $group = '';
        }else {
            $group = 'group by ' . $str['group'];
        }

        $sql = "SELECT $fields FROM $table $where $group $order";

        print_sql_log($sql);

        $show=$this->conn->CacheSelectLimit($sql,$num,$start)->GetAll();
        return $show;
    }

    //删除单笔数据
    public function del_Id($table,$field,$id) {
        if(empty($id)) {
            return false;
        }
        $sql="DELETE FROM $table where $field=$id";

        print_sql_log($sql);

        $this->conn->CacheExecute($sql);
    }

    //sql过滤
    public function sql_put($str)
    {
        if (!get_magic_quotes_gpc()) {
            $sql_str=addslashes(htmlentities($str, ENT_QUOTES, 'GB2312'));
        } else {
            $sql_str=htmlentities($str, ENT_QUOTES, 'GB2312');
        }
        return $sql_str;
    }

    //2014/6/6 add by liangzhiyang 直接执行sql语句
    public function execute($sql)
    {

        print_sql_log($sql);

        $rs=$this->conn->CacheExecute($sql);
        $data = $rs->GetAll();
        return $data;
    }

    //执行查询Sql
    public function query($sql)
    {
        print_sql_log($sql);

        $rs=$this->conn->CacheExecute($sql);
        return $rs->GetAll();
    }

    //关闭数据链接
    public function close() {
        $this->conn->close();
    }

    public function __destruct()
    {
        unset($this->conn);
    }
}